/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmPatientTally.java
 *
 * arthur mbelisle age 24
 *
 * Created on Jul 20, 2011, 4:39:29 PM
 */

package khmhappointmentsystem;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmPatientTally extends javax.swing.JInternalFrame {

    /** Creates new form FrmPatientTally */
    public FrmPatientTally() {
        initComponents();
        setCalendarDate();
        loadSpecialist();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cmbspecialist = new javax.swing.JComboBox();
        calStartDate = new com.toedter.calendar.JDateChooser();
        calEndDate = new com.toedter.calendar.JDateChooser();
        cmdExit = new javax.swing.JButton();
        cmdRunReport = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(khmhappointmentsystem.Main.class).getContext().getResourceMap(FrmPatientTally.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        cmbspecialist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbspecialist.setName("cmbspecialist"); // NOI18N

        calStartDate.setName("calStartDate"); // NOI18N

        calEndDate.setName("calEndDate"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(khmhappointmentsystem.Main.class).getContext().getActionMap(FrmPatientTally.class, this);
        cmdExit.setAction(actionMap.get("Exit")); // NOI18N
        cmdExit.setText(resourceMap.getString("cmdExit.text")); // NOI18N
        cmdExit.setName("cmdExit"); // NOI18N

        cmdRunReport.setAction(actionMap.get("RunReport")); // NOI18N
        cmdRunReport.setText(resourceMap.getString("cmdRunReport.text")); // NOI18N
        cmdRunReport.setName("cmdRunReport"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmbspecialist, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(calStartDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addGap(10, 10, 10)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(cmdRunReport)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(cmdExit))
                                .addComponent(calEndDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbspecialist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(calStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(calEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdRunReport)
                    .addComponent(cmdExit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-297)/2, (screenSize.height-217)/2, 297, 217);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void Exit()
    {
        this.dispose();
    }

    void setCalendarDate()
    {
        calStartDate.setDate(new java.util.Date());
        calEndDate.setDate(new java.util.Date());
    }

    private void loadSpecialist()
    {
        cmbspecialist.removeAllItems();
        cmbspecialist.addItem(" ");
        String query = "SELECT DISTINCT specialist FROM clinic";
        try
        {
            PreparedStatement prep = Environment.getDbConn().prepareStatement(query);
            ResultSet rs = prep.executeQuery();

            while (rs.next())
            {
                cmbspecialist.addItem(rs.getString(1));
            }
            rs.close();
            prep.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Action
    public void RunReport()
    {
         //create object for date
        DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");

        String startDate = MDY_Formatter.format(calStartDate.getDate());
        String endDate = MDY_Formatter.format(calEndDate.getDate());

        String report = "Patient_Tally.jasper";
        String location = java.util.ResourceBundle.getBundle("khmhappointmentsystem.resources/Main").getString("reportFolderLocation");
        report = location + report;

        // Second, create a map of parameters to pass to the report.
        Map parameters = new HashMap();
        parameters.put("beginDate", startDate);
        parameters.put("endDate", endDate);

        if (cmbspecialist.getSelectedIndex() == 0)
        {
            parameters.put("doctor", "%");
        }
        else
        {
            parameters.put("doctor", cmbspecialist.getSelectedItem());
        }

        try
        {
            ReportFill ReportFill = new ReportFill();
            ReportFill.generateReport(report, parameters, Environment.getDbConn(), "Specialist Clinic Tally");
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser calEndDate;
    private com.toedter.calendar.JDateChooser calStartDate;
    private javax.swing.JComboBox cmbspecialist;
    private javax.swing.JButton cmdExit;
    private javax.swing.JButton cmdRunReport;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables

}
